package com.atguigu.admin.controller;

import cn.hutool.core.lang.tree.Tree;
import com.atguigu.admin.model.entity.SysMenu;
import com.atguigu.admin.model.vo.AssginMenuVo;
import com.atguigu.admin.service.SysMenuService;
import com.atguigu.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-10-28
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/admin/sysMenu")
@CrossOrigin
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation(value = "获取菜单")
    @GetMapping("/v1/findNodes")
    public Result findNodes() {
        //List<SysMenu> list = sysMenuService.findNodes();
        List<Tree<Long>> list = sysMenuService.findNodes();
        return Result.ok(list);
    }

    @ApiOperation(value = "新增菜单")
    @PostMapping("/v1/save")
    public Result save(@RequestBody SysMenu permission) {
        boolean isSuccess = sysMenuService.save(permission);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("/v1/update")
    public Result updateById(@RequestBody SysMenu permission) {
        sysMenuService.updateById(permission);
        return Result.ok();
    }

    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/v1/remove/{id}")
    public Result remove(@PathVariable Long id) {
        sysMenuService.removeMenuById(id);
        return Result.ok();
    }

    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("/v1/toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId) {
        List<Tree<Long>> list = sysMenuService.findSysMenuByRoleId(roleId);
        return Result.ok(list);
    }

    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/v1/doAssign")
    public Result doAssign(@RequestBody AssginMenuVo assginMenuVo) {
        sysMenuService.doAssign(assginMenuVo);
        return Result.ok();
    }

}

